package com.itheima52.mobilesafe.activity;

import java.util.List;

import com.itheima52.mobilesafe.R;
import com.itheima52.mobilesafe.bean.BlackNumberInfo;
import com.itheima52.mobilesafe.db.dao.BlackNumberDao;
import com.itheima52.mobilesafe.utils.ToastUtils;

import android.R.bool;
import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class CallSafeActivity extends Activity {
	private ListView list_view;
	private List<BlackNumberInfo> blackNumberInfos;
	private LinearLayout ll_pb;
	private BlackNumberDao dao;
	private CallSafeAdapter adapter;
	private TextView tv_page_number;

	private int mCurrentPageNumber = 0;// 当前页面
	private int mPageSize = 7;// 每一页展示7跳数据
	private int totalPage;// 一共有多少页

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_call_safe);
		initUI();
		initData();
	}

	private Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				// 加载完数据后,将玩命加载隐藏
				ll_pb.setVisibility(View.INVISIBLE);
				adapter = new CallSafeAdapter();
				list_view.setAdapter(adapter);
				tv_page_number.setText(mCurrentPageNumber + "/" + totalPage);

				break;

			default:
				break;
			}

		};

	};
	private EditText et_page_number;

	private void initData() {

		new Thread() {

			public void run() {
				dao = new BlackNumberDao(CallSafeActivity.this);
				// 通过总的记录数/每一页的条数
				totalPage = dao.getTotalNumber() / mPageSize;

				// blackNumberInfos = dao.findAll();
				blackNumberInfos = dao.findPar(mCurrentPageNumber, mPageSize);

				handler.sendEmptyMessage(0);

			};

		}.start();

	}

	private void initUI() {
		ll_pb = (LinearLayout) findViewById(R.id.ll_pb);

		// 展示加载的圆圈
		ll_pb.setVisibility(View.VISIBLE);

		list_view = (ListView) findViewById(R.id.list_view);

		tv_page_number = (TextView) findViewById(R.id.tv_page_number);
		et_page_number = (EditText) findViewById(R.id.et_page_number);

	}

	private class CallSafeAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return blackNumberInfos.size();
		}

		@Override
		public Object getItem(int position) {
			return blackNumberInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = View.inflate(CallSafeActivity.this,
						R.layout.item_call_safe, null);
				holder = new ViewHolder();

				holder.tv_number = (TextView) convertView
						.findViewById(R.id.tv_number);
				holder.tv_mode = (TextView) convertView
						.findViewById(R.id.tv_mode);
				holder.iv_delete = (ImageView) convertView
						.findViewById(R.id.iv_delete);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.tv_number
					.setText(blackNumberInfos.get(position).getNumber());
			String mode = blackNumberInfos.get(position).getMode();
			if (mode.equals("1")) {
				holder.tv_mode.setText("来电+短信拦截");
			} else if (mode.equals("2")) {
				holder.tv_mode.setText("电话拦截");
			} else if (mode.equals("3")) {
				holder.tv_mode.setText("短信拦截");
			}
			final BlackNumberInfo blackNumberInfo = blackNumberInfos
					.get(position);
			holder.iv_delete.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					String number = blackNumberInfo.getNumber();
					boolean result = dao.delete(number);
					if (result) {
						ToastUtils.showToast(CallSafeActivity.this, "删除成功");
						blackNumberInfos.remove(blackNumberInfo);
						adapter.notifyDataSetChanged();
					} else {
						ToastUtils.showToast(CallSafeActivity.this, "删除失败");
					}
				}
			});

			return convertView;
		}

		class ViewHolder {
			TextView tv_number;
			TextView tv_mode;
			ImageView iv_delete;
		}

	}

	/**
	 * 上一页
	 * 
	 * @param view
	 */
	public void prePage(View view) {
		if (mCurrentPageNumber <= 0) {
			ToastUtils.showToast(CallSafeActivity.this, "已经是第一页");
			return;
		}
		mCurrentPageNumber--;
		initData();

	}

	/**
	 * 下一页
	 * 
	 * @param view
	 */
	public void nextPage(View view) {
		// 判断当前页码不能大于总的页数
		if (mCurrentPageNumber >= totalPage) {
			ToastUtils.showToast(CallSafeActivity.this, "已经到最后一页了");
			return;
		}
		mCurrentPageNumber++;

		initData();

	}

	/**
	 * 跳转
	 * 
	 * @param view
	 */
	public void jump(View view) {
		String str_page_number = et_page_number.getText().toString().trim();
		if (TextUtils.isEmpty(str_page_number)) {
			ToastUtils.showToast(CallSafeActivity.this, "请输入正确的页码");

		} else {
			int number = Integer.parseInt(str_page_number);
			if (number >= 0 && number <= (totalPage - 1)) {
				mCurrentPageNumber = number;
				initData();
			} else {
				ToastUtils.showToast(CallSafeActivity.this, "请输入正确的页码");
			}

		}
	}

}
